package com.qfjy.project.meeting;

import com.qfjy.common.util.redis.RepeatUtil;
import com.qfjy.common.util.result.ResultCode;
import com.qfjy.common.util.result.ResultJson;
import com.qfjy.entity.po.MeetingGrab;
import com.qfjy.entity.po.MeetingPub;
import com.qfjy.project.weixin.util.crypto.AesUtil;
import com.qfjy.service.MeetingGrabService;
import com.qfjy.service.MeetingPubService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

@Controller
@RequestMapping("/meetingGrab")
@Slf4j
public class MeetingGrabController {

    /**
     * 会议发单业务逻辑
     */
    @Autowired
    private MeetingPubService meetingPubService;

    @Autowired
    private MeetingGrabService meetingGrabService;
    @Autowired
    private RedisTemplate redisTemplate;

    /***
     * todo 会议抢单 -> 可抢单列表
     */
    @GetMapping("/grabListByUidOrTid")
    @ResponseBody
    public ResultJson grabListByUidOrTid(@RequestParam("encryptUid") String encryptUid,
                                         @RequestParam("tid") String tid) {
        try {

            String uid = AesUtil.encryptHexGet(encryptUid);
            List<MeetingPub> list = meetingPubService.selectMeetingPubGrabListByUidOrTid(uid, tid);
            return new ResultJson(list, ResultCode.SUCCESS);
        } catch (Exception e) {
            return new ResultJson(null, ResultCode.PARAMS_ERROR);
        }

    }

    /**
     * todo 会议抢单- 可抢单列表--》可抢单（按钮） --》进入可抢单页面（添加）
     */
    @GetMapping("/meetingGrabToAddPage")
    public String meetingGrabToAddPage(HttpServletRequest request) {
        request.setAttribute("pid", request.getParameter("pid"));
        request.setAttribute("encryptUid", request.getParameter("encryptUid"));
        return "meetingGrab/meetingGrabAddPage";
    }


    /**
     * todo 会议抢单 -》 可抢单列表--》可抢单（按钮） --》 进入可抢单页面（添加） --》 （进入信息）进行抢单添加
     */
    @PostMapping("/addMeetingGrab")
    @ResponseBody
    public ResultJson addMeetingGrab(MeetingGrab meetingGrab,
                                     HttpServletRequest request) {
        String encryptUid = request.getParameter("encryptUid");
        String pid = meetingGrab.getPid();
        String key = "weixin:meetingGrab:add:";
        key += pid;

        try {

            if (!redisTemplate.hasKey(key)) {
                String uid = AesUtil.encryptHexGet(encryptUid);
                meetingGrab.setUid(Integer.valueOf(uid));
                int num = meetingGrabService.insertSelective(meetingGrab);
                redisTemplate.opsForValue().set(key, pid, 1, TimeUnit.HOURS);
                log.info("会议抢单信息成功{}", meetingGrab);
                return new ResultJson(num, ResultCode.SUCCESS);
            } else {
                return new ResultJson(null, ResultCode.TOO_FREQUENT);
            }
        } catch (Exception e) {
            log.info("会议抢单信息失败{}", meetingGrab);
            return new ResultJson(null, ResultCode.NOT_SUPPORTED);
        }
    }

    @GetMapping("/selectListMeetingGrabByUid")
    @ResponseBody
    public ResultJson selectListMeetingGrabByUid(@RequestParam("encryptUid") final String encryptUid) {
        String uid = AesUtil.encryptHexGet(encryptUid);
        List<MeetingGrab> list = meetingGrabService.selectListMeetingGrabByUid(Integer.parseInt(uid));
        if (list.size() == 0) {
            return new ResultJson(null, ResultCode.NOT_DATA);
        }
        // 可以通过AOP方式进行日志的管理
        return new ResultJson(list, ResultCode.SUCCESS);
    }

}
